{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2a2fc4ba",
   "metadata": {},
   "source": [
    "# 情感分析：使用卷积神经网络\n",
    ":label:`sec_sentiment_cnn`\n",
    "\n",
    "在 :numref:`chap_cnn`中，我们探讨了使用二维卷积神经网络处理二维图像数据的机制，并将其应用于局部特征，如相邻像素。虽然卷积神经网络最初是为计算机视觉设计的，但它也被广泛用于自然语言处理。简单地说，只要将任何文本序列想象成一维图像即可。通过这种方式，一维卷积神经网络可以处理文本中的局部特征，例如$n$元语法。\n",
    "\n",
    "本节将使用*textCNN*模型来演示如何设计一个表示单个文本 :cite:`Kim.2014`的卷积神经网络架构。与 :numref:`fig_nlp-map-sa-rnn`中使用带有GloVe预训练的循环神经网络架构进行情感分析相比， :numref:`fig_nlp-map-sa-cnn`中唯一的区别在于架构的选择。\n",
    "\n",
    "![将GloVe放入卷积神经网络架构进行情感分析](../img/nlp-map-sa-cnn.svg)\n",
    ":label:`fig_nlp-map-sa-cnn`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "56d36db1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('..')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "48191c52",
   "metadata": {},
   "outputs": [],
   "source": [
    "from d2l import mindspore as d2l\n",
    "from mindspore.common import initializer as weight_init\n",
    "from mindspore import nn, ops\n",
    "\n",
    "batch_size = 64\n",
    "train_iter, test_iter, vocab = d2l.load_data_imdb(batch_size)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2d4ebd55",
   "metadata": {},
   "source": [
    "## 一维卷积\n",
    "\n",
    "在介绍该模型之前，让我们先看看一维卷积是如何工作的。请记住，这只是基于互相关运算的二维卷积的特例。\n",
    "\n",
    "![一维互相关运算。阴影部分是第一个输出元素以及用于输出计算的输入和核张量元素：$0\\times1+1\\times2=2$](../img/conv1d.svg)\n",
    ":label:`fig_conv1d`\n",
    "\n",
    "如 :numref:`fig_conv1d`中所示，在一维情况下，卷积窗口在输入张量上从左向右滑动。在滑动期间，卷积窗口中某个位置包含的输入子张量（例如， :numref:`fig_conv1d`中的$0$和$1$）和核张量（例如， :numref:`fig_conv1d`中的$1$和$2$）按元素相乘。这些乘法的总和在输出张量的相应位置给出单个标量值（例如， :numref:`fig_conv1d`中的$0\\times1+1\\times2=2$）。\n",
    "\n",
    "我们在下面的`corr1d`函数中实现了一维互相关。给定输入张量`X`和核张量`K`，它返回输出张量`Y`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c2376bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def corr1d(X, K):\n",
    "    w = K.shape[0]\n",
    "    Y = d2l.zeros((X.shape[0] - w + 1))\n",
    "    for i in range(Y.shape[0]):\n",
    "        Y[i] = (X[i: i + w] * K).sum()\n",
    "    return Y"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "717592d1",
   "metadata": {},
   "source": [
    "我们可以从 :numref:`fig_conv1d`构造输入张量`X`和核张量`K`来验证上述一维互相关实现的输出。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "688c5e7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "X, K = d2l.tensor([0, 1, 2, 3, 4, 5, 6]), d2l.tensor([1, 2])\n",
    "corr1d(X, K)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9e8b3188",
   "metadata": {},
   "source": [
    "对于任何具有多个通道的一维输入，卷积核需要具有相同数量的输入通道。然后，对于每个通道，对输入的一维张量和卷积核的一维张量执行互相关运算，将所有通道上的结果相加以产生一维输出张量。 :numref:`fig_conv1d_channel`演示了具有3个输入通道的一维互相关操作。\n",
    "\n",
    "![具有3个输入通道的一维互相关运算。阴影部分是第一个输出元素以及用于输出计算的输入和核张量元素：$2\\times(-1)+3\\times(-3)+1\\times3+2\\times4+0\\times1+1\\times2=2$](../img/conv1d-channel.svg)\n",
    ":label:`fig_conv1d_channel`\n",
    "\n",
    "我们可以实现多个输入通道的一维互相关运算，并在 :numref:`fig_conv1d_channel`中验证结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e42c7d37",
   "metadata": {},
   "outputs": [],
   "source": [
    "def corr1d_multi_in(X, K):\n",
    "    # 首先，遍历'X'和'K'的第0维（通道维）。然后，把它们加在一起\n",
    "    return sum(corr1d(x, k) for x, k in zip(X, K))\n",
    "\n",
    "X = d2l.tensor([[0, 1, 2, 3, 4, 5, 6],\n",
    "              [1, 2, 3, 4, 5, 6, 7],\n",
    "              [2, 3, 4, 5, 6, 7, 8]])\n",
    "K = d2l.tensor([[1, 2], [3, 4], [-1, -3]])\n",
    "corr1d_multi_in(X, K)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a1c49c7",
   "metadata": {},
   "source": [
    "注意，多输入通道的一维互相关等同于单输入通道的二维互相关。举例说明， :numref:`fig_conv1d_channel`中的多输入通道一维互相关的等价形式是 :numref:`fig_conv1d_2d`中的单输入通道二维互相关，其中卷积核的高度必须与输入张量的高度相同。\n",
    "\n",
    "![具有单个输入通道的二维互相关操作。阴影部分是第一个输出元素以及用于输出计算的输入和内核张量元素： $2\\times(-1)+3\\times(-3)+1\\times3+2\\times4+0\\times1+1\\times2=2$](../img/conv1d-2d.svg)\n",
    ":label:`fig_conv1d_2d`\n",
    "\n",
    " :numref:`fig_conv1d`和 :numref:`fig_conv1d_channel`中的输出都只有一个通道。与 :numref:`subsec_multi-output-channels`中描述的具有多个输出通道的二维卷积相同，我们也可以为一维卷积指定多个输出通道。\n",
    "\n",
    "## 最大时间汇聚层\n",
    "\n",
    "类似地，我们可以使用汇聚层从序列表示中提取最大值，作为跨时间步的最重要特征。textCNN中使用的*最大时间汇聚层*的工作原理类似于一维全局汇聚 :cite:`Collobert.Weston.Bottou.ea.2011`。对于每个通道在不同时间步存储值的多通道输入，每个通道的输出是该通道的最大值。请注意，最大时间汇聚允许在不同通道上使用不同数量的时间步。\n",
    "\n",
    "## textCNN模型\n",
    "\n",
    "使用一维卷积和最大时间汇聚，textCNN模型将单个预训练的词元表示作为输入，然后获得并转换用于下游应用的序列表示。\n",
    "\n",
    "对于具有由$d$维向量表示的$n$个词元的单个文本序列，输入张量的宽度、高度和通道数分别为$n$、$1$和$d$。textCNN模型将输入转换为输出，如下所示：\n",
    "\n",
    "1. 定义多个一维卷积核，并分别对输入执行卷积运算。具有不同宽度的卷积核可以捕获不同数目的相邻词元之间的局部特征。\n",
    "1. 在所有输出通道上执行最大时间汇聚层，然后将所有标量汇聚输出连结为向量。\n",
    "1. 使用全连接层将连结后的向量转换为输出类别。Dropout可以用来减少过拟合。\n",
    "\n",
    "![textCNN的模型架构](../img/textcnn.svg)\n",
    ":label:`fig_conv1d_textcnn`\n",
    "\n",
    " :numref:`fig_conv1d_textcnn`通过一个具体的例子说明了textCNN的模型架构。输入是具有11个词元的句子，其中每个词元由6维向量表示。因此，我们有一个宽度为11的6通道输入。定义两个宽度为2和4的一维卷积核，分别具有4个和5个输出通道。它们产生4个宽度为$11-2+1=10$的输出通道和5个宽度为$11-4+1=8$的输出通道。尽管这9个通道的宽度不同，但最大时间汇聚层给出了一个连结的9维向量，该向量最终被转换为用于二元情感预测的2维输出向量。\n",
    "\n",
    "### 定义模型\n",
    "\n",
    "我们在下面的类中实现textCNN模型。与 :numref:`sec_sentiment_rnn`的双向循环神经网络模型相比，除了用卷积层代替循环神经网络层外，我们还使用了两个嵌入层：一个是可训练权重，另一个是固定权重。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2ed6c15d",
   "metadata": {},
   "outputs": [],
   "source": [
    "class TextCNN(nn.Cell):\n",
    "    def __init__(self, vocab_size, embed_size, kernel_sizes, num_channels,\n",
    "                 **kwargs):\n",
    "        super(TextCNN, self).__init__(**kwargs)\n",
    "        self.embedding = nn.Embedding(vocab_size, embed_size)\n",
    "        # 这个嵌入层不需要训练\n",
    "        self.constant_embedding = nn.Embedding(vocab_size, embed_size)\n",
    "        self.dropout = nn.Dropout(p=0.5)\n",
    "        self.decoder = nn.Dense(sum(num_channels), 2)\n",
    "        # 最大时间汇聚层没有参数，因此可以共享此实例\n",
    "        self.pool = nn.AdaptiveAvgPool1d(1)\n",
    "        self.relu = nn.ReLU()\n",
    "        # 创建多个一维卷积层\n",
    "        self.convs = nn.CellList()\n",
    "        for c, k in zip(num_channels, kernel_sizes):\n",
    "            self.convs.append(nn.Conv1d(2 * embed_size, c, k))\n",
    "\n",
    "    def construct(self, inputs):\n",
    "        # 沿着向量维度将两个嵌入层连结起来，\n",
    "        # 每个嵌入层的输出形状都是（批量大小，词元数量，词元向量维度）连结起来\n",
    "        embeddings = ops.cat((\n",
    "            self.embedding(inputs), self.constant_embedding(inputs)), axis=2)\n",
    "        # 根据一维卷积层的输入格式，重新排列张量，以便通道作为第2维\n",
    "        embeddings = embeddings.transpose(0, 2, 1)\n",
    "        # 每个一维卷积层在最大时间汇聚层合并后，获得的张量形状是（批量大小，通道数，1）\n",
    "        # 删除最后一个维度并沿通道维度连结\n",
    "        encoding = ops.cat([\n",
    "            ops.squeeze(self.relu(self.pool(conv(embeddings))), axis=-1)\n",
    "            for conv in self.convs], axis=1)\n",
    "        outputs = self.decoder(self.dropout(encoding))\n",
    "        return outputs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2397771",
   "metadata": {},
   "source": [
    "让我们创建一个textCNN实例。它有3个卷积层，卷积核宽度分别为3、4和5，均有100个输出通道。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "163fbfce",
   "metadata": {},
   "outputs": [],
   "source": [
    "embed_size, kernel_sizes, nums_channels = 100, [3, 4, 5], [100, 100, 100]\n",
    "net = TextCNN(len(vocab), embed_size, kernel_sizes, nums_channels)\n",
    "\n",
    "def init_weights(net):\n",
    "    init_normal = weight_init.XavierUniform()\n",
    "    for _, cell in net.cells_and_names():\n",
    "        if isinstance(cell, (nn.Dense, nn.Conv1d)):\n",
    "            cell.weight.set_data(weight_init.initializer(init_normal,\n",
    "                                                         cell.weight.shape,\n",
    "                                                         cell.weight.dtype))\n",
    "            \n",
    "init_weights(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e96a8013",
   "metadata": {},
   "source": [
    "### 加载预训练词向量\n",
    "\n",
    "与 :numref:`sec_sentiment_rnn`相同，我们加载预训练的100维GloVe嵌入作为初始化的词元表示。这些词元表示（嵌入权重）在`embedding`中将被训练，在`constant_embedding`中将被固定。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f81ba9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "glove_embedding = d2l.TokenEmbedding('glove.6b.100d')\n",
    "embeds = glove_embedding[vocab.idx_to_token]\n",
    "net.embedding.embedding_table.set_data(d2l.tensor(embeds))\n",
    "net.constant_embedding.embedding_table.set_data(d2l.tensor(embeds))\n",
    "net.constant_embedding.embedding_table.requires_grad = False"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55e18a54",
   "metadata": {},
   "source": [
    "### 训练和评估模型\n",
    "\n",
    "现在我们可以训练textCNN模型进行情感分析。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3358c347",
   "metadata": {},
   "outputs": [],
   "source": [
    "lr, num_epochs = 0.001, 5\n",
    "trainer = nn.Adam(params=net.trainable_params(), lr=lr)\n",
    "loss = nn.CrossEntropyLoss(reduction=\"none\")\n",
    "d2l.train_ch13(net, train_iter, test_iter, loss, trainer, num_epochs)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb10fb87",
   "metadata": {},
   "source": [
    "下面，我们使用训练好的模型来预测两个简单句子的情感。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39004eb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "d2l.predict_sentiment(net, vocab, 'this movie is so great')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "940726d0",
   "metadata": {},
   "outputs": [],
   "source": [
    "d2l.predict_sentiment(net, vocab, 'this movie is so bad')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2611d946",
   "metadata": {},
   "source": [
    "## 小结\n",
    "\n",
    "* 一维卷积神经网络可以处理文本中的局部特征，例如$n$元语法。\n",
    "* 多输入通道的一维互相关等价于单输入通道的二维互相关。\n",
    "* 最大时间汇聚层允许在不同通道上使用不同数量的时间步长。\n",
    "* textCNN模型使用一维卷积层和最大时间汇聚层将单个词元表示转换为下游应用输出。\n",
    "\n",
    "## 练习\n",
    "\n",
    "1. 调整超参数，并比较 :numref:`sec_sentiment_rnn`中用于情感分析的架构和本节中用于情感分析的架构，例如在分类精度和计算效率方面。\n",
    "1. 请试着用 :numref:`sec_sentiment_rnn`练习中介绍的方法进一步提高模型的分类精度。\n",
    "1. 在输入表示中添加位置编码。它是否提高了分类的精度？\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
